import { defineStore } from "pinia";
import { UserState } from "../interface";
import piniaPersistConfig from "@/stores/helper/persist";

export const useUserStore = defineStore({
  id: "user",
  state: (): UserState => ({
    token: localStorage.getItem("token") || "",
    userInfo: { username: "" },
  }),
  getters: {},
  actions: {
    /**
     * @use 保存token
     */
    setToken(token: string) {
      this.token = token;
    },

    /**
     * @use 保存用户名密码
     * @param userInfo 用户名
     */
    setUserInfo(userInfo: UserState["userInfo"]) {
      this.userInfo = userInfo;
    },
  },
  persist: piniaPersistConfig(`user`),
});
